﻿using Ma.Bussiness.AttributeExtend;
using Ma.Bussiness.ModelExtend;
using System.Data.SqlClient;

namespace Ma.Bussiness.DBExtend.SQLQueryExtend;

/// <summary>
/// 泛型拼接SQL语句执行集
/// </summary>
public class FuncDBHelper : IBaseService
{
    /// <summary>
    /// 新增实体
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="t"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public bool Add<T>(T t) where T : BaseModel
    {
        Type type = typeof(T);
        var props = type.GetProperties().Where(p => p.Name != "Id");
        var parameters = props.Select(p => new SqlParameter($"@{p.GetMappingColumnName()}", p.GetValue(t) ?? DBNull.Value)).ToArray();
        string sql = $"{TSqlHelper<T>.AddSql}";
        //using (SqlConnection conn = new SqlConnection(StaticField.Connection))
        //{
        // SqlCommand sqlCommand = new SqlCommand(sql, conn);
        // sqlCommand.Parameters.AddRange(parameters);
        // conn.Open();
        //int result=  sqlCommand.ExecuteNonQuery();
        // if (result == 0)
        //     throw new Exception("新增异常");
        // return true;

        // }
        return ExecuteHelper.Command(sql, sqlCommand => {
            sqlCommand.Parameters.AddRange(parameters);
            int result = sqlCommand.ExecuteNonQuery();
            if (result == 0)
                throw new Exception("新增异常");
            return true;
        });
    }
    /// <summary>
    /// 删除实体
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="t"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public bool Delete<T>(T t) where T : BaseModel
    {
        if (t.Id == 0) throw new Exception("删除数据不存在");
        Type type = typeof(T);
        string sql = $"{TSqlHelper<T>.DeleteSql}  WHERE Id={t.Id}";
        //using (SqlConnection conn = new SqlConnection(StaticField.Connection))
        //{
        //    SqlCommand sqlCommand = new SqlCommand(sql, conn);
        //    conn.Open();
        //    int result = sqlCommand.ExecuteNonQuery();
        //    if (result == 0)
        //        throw new Exception("删除异常");
        //    return true;
        //}

        return ExecuteHelper.Command(sql, sqlCommand => {
            int result = sqlCommand.ExecuteNonQuery();
            if (result == 0)
                throw new Exception("删除异常");
            return true;
        });
    }

    /// <summary>
    /// 查找单个数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="id"></param>
    /// <returns></returns>
    public T FindT<T>(int id) where T : BaseModel
    {
        Type type = typeof(T);
        string sql = $"{TSqlHelper<T>.FindSql}  WHERE Id={id}";
        List<T> list = ExecuteHelper.ExecuteReader<T>(sql);
        return list.FirstOrDefault();
    }

    /// <summary>
    /// 查找全部
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public List<T> FindAll<T>() where T : BaseModel
    {
        Type type = typeof(T);
        string sql = $"{TSqlHelper<T>.FindSql}";
        return ExecuteHelper.ExecuteReader<T>(sql);
    }


    public bool Update<T>(T t) where T : BaseModel
    {
        if (t.Id == 0) throw new Exception("修改数据不存在");
        Type type = typeof(T);
        var props = type.GetProperties().Where(p => !p.Name.Equals("Id"));
        string sql = $"{TSqlHelper<T>.EditSql} WHERE Id={t.Id}";
        var parameters = props.Select(p => new SqlParameter($"@{p.GetMappingColumnName()}", p.GetValue(t) ?? DBNull.Value)).ToArray();
        //using (SqlConnection conn = new SqlConnection(StaticField.Connection))
        //{
        //    SqlCommand sqlCommand = new SqlCommand(sql, conn);
        //    sqlCommand.Parameters.AddRange(parameters);
        //    conn.Open();
        //    int result = sqlCommand.ExecuteNonQuery();
        //    if (result == 0)
        //        throw new Exception("修改异常");
        //    return true;
        //}
        return ExecuteHelper.Command(sql, sqlCommand => {
            sqlCommand.Parameters.AddRange(parameters);
            int result = sqlCommand.ExecuteNonQuery();
            if (result == 0)
                throw new Exception("修改异常");
            return true;
        });
    }
}

public interface IBaseService
{
    T FindT<T>(int id) where T : BaseModel;
    List<T> FindAll<T>() where T : BaseModel;
    bool Add<T>(T t) where T : BaseModel;
    bool Update<T>(T t) where T : BaseModel;
    bool Delete<T>(T t) where T : BaseModel;
}
